{{\def\dash{\raise2.1pt\hbox{\rule{5pt}{0.3pt}}\hspace{1pt}}\begin{tabbing}
\makebox[96pt][l]{{\textbf{\#define}}\hspace{6pt}N}\makebox[48pt][l]{100}\makebox[48pt][l]{}{\it{/$\ast$\hspace{6pt}缓冲区中槽的总数\hspace{6pt}$\ast$/}}\\
\makebox[96pt][l]{{\textbf{\#define}}\hspace{6pt}TRUE}1\\
\makebox[96pt][l]{{\textbf{typedef}}\hspace{6pt}{\textbf{int}}}\makebox[96pt][l]{semaphore;}{\it{/$\ast$\hspace{6pt}信号量其实就是一类整型数\hspace{6pt}$\ast$/}}\\
\makebox[96pt][l]{semaphore}mutex\hspace{6pt}=\hspace{6pt}1;\\
\makebox[96pt][l]{semaphore}\makebox[96pt][l]{empty\hspace{6pt}=\hspace{6pt}N;}{\it{/$\ast$\hspace{6pt}缓冲区中空槽的数量\hspace{6pt}$\ast$/}}\\
\makebox[96pt][l]{semaphore}\makebox[96pt][l]{full\hspace{12pt}=\hspace{6pt}0;}{\it{/$\ast$\hspace{6pt}缓冲区中满槽的数量\hspace{6pt}$\ast$/}}\\
\\
{\textbf{void}}\\
producer\hspace{6pt}({\textbf{void}})\\
\{\\
\hspace{24pt}{\textbf{int}}\hspace{6pt}item;\\
\\
\hspace{24pt}{\textbf{while}}\hspace{6pt}(TRUE)\hspace{6pt}\{\\
\makebox[48pt][l]{}item\hspace{6pt}=\hspace{6pt}produce\_item\hspace{6pt}();\hspace{6pt}{\it{/$\ast$\hspace{6pt}生成元素\hspace{6pt}$\ast$/}}\\
\makebox[48pt][l]{}p\hspace{6pt}(\&empty);\hspace{6pt}{\it{/$\ast$\hspace{6pt}尝试减少空槽的数目\hspace{6pt}$\ast$/}}\\
\\
\makebox[48pt][l]{}p\hspace{6pt}(\&mutex);\hspace{6pt}{\it{/$\ast$\hspace{6pt}尝试获取互斥锁进入临界区\hspace{6pt}$\ast$/}}\\
\makebox[48pt][l]{}insert\_item\hspace{6pt}(item);\hspace{6pt}{\it{/$\ast$\hspace{6pt}将元素放入缓冲区\hspace{6pt}$\ast$/}}\\
\makebox[48pt][l]{}v\hspace{6pt}(\&mutex);\hspace{6pt}{\it{/$\ast$\hspace{6pt}离开临界区\hspace{6pt}$\ast$/}}\\
\\
\makebox[48pt][l]{}v\hspace{6pt}(\&full);\hspace{6pt}{\it{/$\ast$\hspace{6pt}增加满槽的数量\hspace{6pt}$\ast$/}}\\
\hspace{24pt}\}\\
\}\\
\\
{\textbf{void}}\\
consumer\hspace{6pt}({\textbf{void}})\\
\{\\
\hspace{24pt}{\textbf{int}}\hspace{6pt}item;\\
\\
\hspace{24pt}{\textbf{while}}\hspace{6pt}(TRUE)\hspace{6pt}\{\\
\makebox[48pt][l]{}p\hspace{6pt}(\&full);\hspace{6pt}{\it{/$\ast$\hspace{6pt}尝试减少满槽的数量\hspace{6pt}$\ast$/}}\\
\\
\makebox[48pt][l]{}p\hspace{6pt}(\&mutex);\hspace{6pt}{\it{/$\ast$\hspace{6pt}尝试进入临界区\hspace{6pt}$\ast$/}}\\
\makebox[48pt][l]{}item\hspace{6pt}=\hspace{6pt}remove\_item\hspace{6pt}(item);\hspace{6pt}{\it{/$\ast$\hspace{6pt}将元素取出缓冲区\hspace{6pt}$\ast$/}}\\
\makebox[48pt][l]{}v\hspace{6pt}(\&mutex);\hspace{6pt}{\it{/$\ast$\hspace{6pt}离开临界区\hspace{6pt}$\ast$/}}\\
\\
\makebox[48pt][l]{}v\hspace{6pt}(\&empty);\hspace{6pt}{\it{/$\ast$\hspace{6pt}增加空槽的数量\hspace{6pt}$\ast$/}}\\
\hspace{24pt}\}\\
\}
\end{tabbing}}}
